Page({
  data(){
    return {
      params: {},
      payInfo: '',
      paySuccess: false,
      payResultInfo: {},
    }
  },
  onLoad(options){
    this.setData({
      params: options
    })
    this.getPaymentInfo();
  },
  onShow(){
    if(this.data.payInfo){
      const p = JSON.parse(this.data.payInfo);
      wx.request({
        url: `https://wk.kuai-d.com/api/app/we-chat/pay-result/${this.data.params.deviceid}?zfdh=${p.zfdh}`,
        success: (res) => {
          if(res.data.payResult.code === '000000'){
            this.setData({
              paySuccess: true,
              payResultInfo: res.data.payRecordData
            })
          }else if(res.data.payResult.code === '222222'){
            this.getPaymentInfo()
          }else{
            wx.showToast({
              title: res.msg,
            })
            this.getPaymentInfo();
          }
        }
      })
    }
  },

  // 获取支付参数， 并打开半屏小程序支付
  getPaymentInfo(){
    wx.request({
      url: `https://wk.kuai-d.com/api/app/we-chat/pay-info?amount=${this.data.params.amount}&payTitle=${this.data.params.payTitle}`,
      success: (res) => {
        this.setData({
          payInfo: res.data.data
        })
      }
    })
  },
  // 去支付
  toPay(){
    var d = encodeURIComponent(this.data.payInfo);
    console.log(this.data.payInfo)
    console.log(d)
    wx.openEmbeddedMiniProgram({
      appId:"wx863eb7332d2d2849", //需要唤起的半屏小程序的appid
      path:'/pages/embedded/embedded?d=' + d,//唤起的页面，并拼接上参数
      success(){
        console.log('成功')
      }
    })
  },

  back(){
    wx.navigateBack();
  },
})